PHP - Syntax
PHP is a programming language with many features that can easily be embedded in HTML. You can add the PHP code anywhere in the document either within the PHP tags or within the HTML. As you have now done with the configuration part, we can easily start with the concepts. Before we step ahead, we should know what syntax is used in PHP and what is the pattern used by PHP codes to make it easy and understandable. We will now discuss the various type of PHP syntax in this tutorial.
1. Use of PHP tags or Escaping to PHP
You can separate your HTML and PHP code using a process called Escaping to PHP. You can apply this process in a number of ways. Some methods are used by default but for some methods, the .ini file needs some modification. These tags are not only confined to separate both the codes but you can also embed PHP within HTML using below tags.
Tags | Syntax |
SGML or short HTML tags (set open_short_tag setting to ‘ON’ in .ini file) |
<? Echo “hello”; ?> |
Canonical PHP tags |
<?php Echo “hello”; ?> |
HTML script tags (removed in PHP 7.0.0) |
<script language= “php”> Echo “hello”; </script> |
ASP style tags (set settings ‘ON’ in .ini file and describe code block which is used by the active server) |
<% Echo “hello”; %> |
2. Comments in PHP
Comments are basically used to make the code more readable by other programmers or developers. These comments do not parse by the PHP processor and are ignored from being processed. You can add statements that will describe the code without impacting your script results. PHP comes with two types of comments and these are very common practices of using comments.
Single Line Comment | Multi-line comment |
These are short commented statements that you can add within your code. These comments can be started with # or //. | If you want to include a comment that is more than a single line then the # or // will not work. instead of using single comments at the start of each line, it is better that you use multiple line comments for more readability. The statements can be enclosed within /*…..*/ |
Syntax
<?php // first program Echo “hello”; #first program ends here ?> |
Syntax
<?php /* hello I am starting my first program. Enjoy learning. */ Echo “hello”; ?> |
3. Insensitive to whitespace
PHP ignores the white spaces within the code; it may be a single space, a number of spaces, tab or carriage return. Every blank space will not be processed by the PHP parser until it encounters the semicolon to indicate the end of the statement.
<?php $num1= 15; $num2= 5; $sum= $num1 + $num2 ; Echo $sum; $sum=$num1+$num2; Echo $sum; ?>
Output 20 20
4. Case sensitivity
Every variable you define or even the pre-defined variables are all case sensitive except the keywords, functions and methods that are defined within the PHP code.
<?php $var=10; Echo $var; ECHO $var; echo $var; echo $VAR
Output 10 10 10 there is no output for the last ‘VAR; as it is in capital letters which makes it different from the defined variable and PHP parser will not recognize this variable.
5. PHP blocks
If you are using conditional statements then you can use curly-braces blocks of statements that will be executed simultaneously by the PHP parser.
<?php $num1=2; If (var %2==0) { Echo “even number”; Else Echo “odd number”; } ?>
Output even number Above is the basic syntax that you should be aware of by now. After this tutorial, you will be able to make the correct use of this syntax within your code.
People are also reading: